﻿using System;
using XCI.Helper;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 服务端系统操作接口
    /// </summary>
    public class SysService : BaseService<object>
    {
        public static readonly SysService Instance = new SysService();
        protected override string RootUrl => "/api/sys/system/";

        /// <summary>
        /// 客户端与服务端网络测试
        /// </summary>
        public bool Ping()
        {
            try
            {
                var url = GetUrl("ping");
                var result = SysRuntime.Api.Post<string>(url).Data;
                return !string.IsNullOrEmpty(result);
            }
            catch (Exception e)
            {
                DebugHelper.Catch(DebugHelper.GetExceptionAllMessage(e));
                return false;
            }
        }

        /// <summary>
        /// 获取服务器名称
        /// </summary>
        public string GetName()
        {
            try
            {
                var url = GetUrl("name");
                return SysRuntime.Api.Post<string>(url).Data;
            }
            catch (Exception e)
            {
                DebugHelper.Catch(DebugHelper.GetExceptionAllMessage(e));
                return "无法连接服务器";
            }
        }

        /// <summary>
        /// 获取服务器日期和时间
        /// </summary>
        public string GetDatetime()
        {
            var url = GetUrl("datetime");
            return SysRuntime.Api.Post<string>(url).Data;
        }

        /// <summary>
        /// 创建新Id
        /// </summary>
        public string GetId()
        {
            var url = GetUrl("newId");
            return SysRuntime.Api.Post<string>(url).EnsureSuccess().Data;
        }     
    }
}